package com.example.blog.controller;

import com.example.blog.config.page.PageRequest;
import com.example.blog.config.page.PageResult;
import com.example.blog.entity.Tag;
import com.example.blog.service.TagService;
import com.example.blog.util.PageUtil;
import com.example.blog.util.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: lu
 * @Date: 2024-01-15-17:17
 * @Description:
 */
@Api(tags = "文章标签")
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    TagService tagService;

    @PostMapping("/find")
    public Result<Object>  findTag(@RequestBody PageRequest pageRequest){
        List<Tag> tags = tagService.find(pageRequest);
        PageInfo<Tag> tagPageInfo = new PageInfo<>(tags);
        PageResult pageResult = PageUtil.getPageResult(pageRequest, tagPageInfo);
        return Result.success(pageResult);
    }

    @PostMapping("/create")
    public  Result<Object> createTag(@RequestBody  Tag tag){
        tagService.create(tag);
        return Result.success(tag);
    }

    @PostMapping("/update")
    public Result<Object> updateTage(@RequestBody Tag tag){
        tagService.update(tag);
        return Result.success(tag);
    }

    @PostMapping("/delete/{id}")
    public  Result<Object> deleteTag(@PathVariable Integer id){
        int delete = tagService.delete(id);
        return Result.success(delete);
    }


    @ApiOperation(value = "批量添加标签")
    @PostMapping("/batchCreate")
    public Result<Object> batchCreate(@RequestBody  Tag tags) {
        try {
            boolean isStatus = tagService.batchAddTag(tags.getTagName());
            if (!isStatus) {
                return Result.error("批量插入失败！");
            }
        }catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }




}
